package leetcode.code0991;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int brokenCalc(int startValue, int target) {
		long plus = 1, value = startValue;
		long ans = 0;
		while (value < target) {
			value *= 2l;
			plus *= 2;
			ans += 1;
		}
		if (value == target) {
			return (int) ans;
		}
		long more = value - target;
		if (more == 1) {
			return (int) ans + 1;
		}
		long add = more % 2;
		long sub = more - add;
		while (sub != 0) {
			if (sub >= plus) {
				long t = sub / plus;
				ans += t;
				sub -= t * plus;
			} else {
				plus /= 2;
			}
		}
		return (int) (ans + add);
	}

	@Override
	public void debug4() {
		H.compare(39, this.brokenCalc(1, 1000000000));
		H.compare(341290, this.brokenCalc(822231, 123123327));
		H.compare(5, this.brokenCalc(8, 25));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
